﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    public partial class UIWGA : ViewBase
    {    
        public VWASTGA TMV
        {
            get
            {
                return (VWASTGA)MV;
            }
        }
        #region ==RWBRT===========
        public static readonly DependencyProperty RWBRTProperty = DependencyProperty.Register("RWBRT",
            typeof(double), typeof(UIWGA),
            new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnRWBRTChanged));
        public double RWBRT
        {
            get
            {
                return (double)GetValue(RWBRTProperty);
            }
            set
            {
                SetValue(RWBRTProperty, value);
                OnRWBRTChg();
            }
        }
        private static void OnRWBRTChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIWGA;
            if (src != null)
            {
                src.RWBRT = (double)e.NewValue;
            }
        }
        public virtual void OnRWBRTChg()
        { }
        #endregion
        #region ==RBRTOL===========
        public static readonly DependencyProperty RBRTOLProperty = DependencyProperty.Register("RBRTOL",
            typeof(double), typeof(UIWGA),
            new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnRBRTOLChanged));
        public double RBRTOL
        {
            get
            {
                return (double)GetValue(RBRTOLProperty);
            }
            set
            {
                SetValue(RBRTOLProperty, value);
                OnRBRTOLChg();
            }
        }
        private static void OnRBRTOLChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIWGA;
            if (src != null)
            {
                src.RBRTOL = (double)e.NewValue;
            }
        }
        public virtual void OnRBRTOLChg()
        { }
        #endregion
        #region ==GFDISM===========
        public static readonly DependencyProperty GFDISMProperty = DependencyProperty.Register("GFDISM",
        typeof(double), typeof(UIWGA),
        new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnGFDISMChanged));
        public double GFDISM
        {
            get
            {
                return (double)GetValue(GFDISMProperty);
            }
            set
            {
                SetValue(GFDISMProperty, value);
                OnGFDISMChg(value);
            }
        }
        private static void OnGFDISMChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIWGA;
            if (src != null)
            {
                src.GFDISM = (double)e.NewValue;
            }
        }
        public virtual void OnGFDISMChg(double value)
        {
             
        }
        #endregion
        #region ==WTKEY===========
        public static readonly DependencyProperty WTKEYProperty = DependencyProperty.Register("WTKEY",
        typeof(string), typeof(UIWGA),
        new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnWTKEYChanged));
        public string WTKEY
        {
            get
            {
                return (string)GetValue(WTKEYProperty);
            }
            set
            {
                SetValue(WTKEYProperty, value);
                OnWTKEYChg();
            }
        }
        private static void OnWTKEYChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIWGA;
            if (src != null)
            {
                src.WTKEY = (string)e.NewValue;
            }
        }
        public virtual void OnWTKEYChg()
        { }
        #endregion
        #region ==FAKTOR===========
        public static readonly DependencyProperty FAKTORProperty = DependencyProperty.Register("FAKTOR",
        typeof(double), typeof(UIWGA),
        new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnFAKTORChanged));
        public double FAKTOR
        {
            get
            {
                return (double)GetValue(FAKTORProperty);
            }
            set
            {
                SetValue(FAKTORProperty, value);
                OnFAKTORChg();
            }
        }
        private static void OnFAKTORChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIWGA;
            if (src != null)
            {
                src.FAKTOR = (double)e.NewValue;
            }
        }
        public virtual void OnFAKTORChg()
        { }
        #endregion
        #region ==YARN===========
        public static readonly DependencyProperty YARNProperty = DependencyProperty.Register("YARN",
            typeof(string), typeof(UIWGA),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnYARNChanged));
        public string YARN
        {
            get
            {
                return (string)GetValue(YARNProperty);
            }
            set
            {
                SetValue(YARNProperty, value);
                OnYARNChg();
            }
        }
        private static void OnYARNChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIWGA;
            if (src != null)
            {
                src.YARN = (string)e.NewValue;
            }
        }
      
        #endregion
        #region ==DENUM===========
        public static readonly DependencyProperty DENUMProperty = DependencyProperty.Register("DENUM",
        typeof(string), typeof(UIWGA),
        new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnDENUMChanged));
        public string DENUM
        {
            get
            {
                return (string)GetValue(DENUMProperty);
            }
            set
            {
                SetValue(DENUMProperty, value); 
            }
        }
        private static void OnDENUMChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIWGA;
            if (src != null)
            {
                src.DENUM = (string)e.NewValue;
            }
        }
        #endregion
        #region ==IsFromWT===========
        public static readonly DependencyProperty IsFromWTProperty = DependencyProperty.Register("IsFromWT",
        typeof(bool), typeof(UIWGA),
        new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsFromWTChanged));
        public bool IsFromWT
        {
            get
            {
                return (bool)GetValue(IsFromWTProperty);
            }
            set
            {
                SetValue(IsFromWTProperty, value);
            }
        }
        private static void OnIsFromWTChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIWGA;
            if (src != null)
            {
                src.IsFromWT = (bool)e.NewValue;
            }
        }
        #endregion

        #region ==WEIGHT===========
        public static readonly DependencyProperty WEIGHTProperty = DependencyProperty.Register("WEIGHT",
        typeof(double), typeof(UIWGA),
        new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnWEIGHTChanged));
        public double WEIGHT
        {
            get
            {
                return (double)GetValue(WEIGHTProperty);
            }
            set
            {
                SetValue(WEIGHTProperty, value);

            }
        }
        private static void OnWEIGHTChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIWGA;
            if (src != null)
            {
                src.WEIGHT = (double)e.NewValue;
                src.OnWEIGHTChg((double)e.NewValue);
            }
        }
        public virtual void OnWEIGHTChg(double value)
        {

        }
        #endregion
        public UIWGA()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
        }

        public int pos = 0;
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            if (dwMaster == null)
            {
                KEY = "";
                YARN = "";
            }
            else
            {  
                KEY = "R"+TMV.WAGART + TMV.WAGVAR+TMV.WAGWCOL+TMV.WAGPSA+TMV.WAGPOS;
                YARN = "G"+TMV.WAGGART + TMV.WAGGVAR + TMV.WAGGFARBE;
            }
            if (dwMaster != null)
            {

                filter = "WAGART='" + dwMaster["WAGART"].ToString() +"'";
                sort = "WAGPOS DESC";
                rows = TBMain.Select(filter, sort);
                foreach (DataRow row in rows)
                { 
                    pos = int.Parse(row["WAGPOS"].ToString());
                    break;
                }
            }
        }
        public override void DeleteCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            if (TBMain.Rows.Count == 1)
                return;
            TMV.DWG01(dwMaster);
            filter = "WAGPOS='" + dwMaster["WAGPOS"].ToString() + "'";
            rows = TBMain.Select(filter);
            vwMaster.View.MoveCurrentTo(-1);
            foreach (DataRow row in rows)
                row.Delete(); 
            vwMaster.View.MoveCurrentToFirst();
            TBMain.AcceptChanges();
            Validate();
            base.DeleteCommand(sender, e);
        }
        public override void OnSave()
        {
            if (!IsSaved)
                return;
            if (!Validate())
                return;
            foreach (DataRow row in TBMain.Rows)
            {
                TMV.UpdateWASTGA(row, 0);
            }
            base.OnSave();
        }
        public bool Validate()
        {
            bool IsValidate = true;
            double ends = 0;
            double wastage = 0;
            double wlg = 0;
            double beamlg = 100;
            double K = 0;
            double qty = 0;
            double FDZ = 0;
            double width = RBRTOL * 0.1 * 0.01;//RBRTOL机上幅宽，RWBRT坯布幅宽
            double density = 0;
            if (DENUM == "  ")
                throw new Exception("请定义纬密单位");

            switch (DENUM.Trim())
            {
                case "INC":
                    density = GFDISM / 2.54;
                    break;
                default:
                case "CM":
                    density = GFDISM;
                    break;
                case "TCM":
                    density = GFDISM / 10;
                    break;
            }

            double weftdensityON = density * 0.01 * 100;//每米内根数，机上

            if (RBRTOL == 0)
            {
                MessageBox.Show("请输入坯布幅宽");
                return false;
            }
            if (GFDISM == 0)
            {
                MessageBox.Show("请输入机上纬密");
                return false;
            }
            foreach (DataRow row in TBMain.Rows)
            {
                FDZ += Convert.ToDouble(row["WAGFDZ"].ToString());
            }
            foreach (DataRow row in TBMain.Rows)
            {
                ART = row["WAGGART"].ToString() + row["WAGGVAR"].ToString() + row["WAGGFARBE"].ToString();
                PM["CART"] = ART;
                SResult = TMV.VGS.LOAD(PM);
                K = 0;
                foreach (DataRow dwGS in SResult.Tables[0].Rows)
                {
                    string ctyp = dwGS["GSRECHNUM"].ToString().Trim();
                    K = Convert.ToDouble(dwGS["GSRECHNR"].ToString()) * 0.0001;//每克重纱的米数
                    K = UMHelper.GPKM(ctyp, K,app.company);
                }
                if (K == 0)
                    continue;
                ends = Convert.ToDouble(row["WAGFDZ"].ToString());
                if (ends == 0)
                    continue;
                wastage = Convert.ToDouble(row["WAGABF"].ToString()) * 0.1 * 0.01;
                wlg = Convert.ToDouble(row["WAGABFLG"].ToString()) * 0.01 * 0.01;//米
                qty = Helper.RoundX(beamlg * weftdensityON * width * (ends / FDZ) * K / 1000, 100);
                row["WAGKGNET"] = qty * 0.001 * 1000;
                qty = Helper.RoundX((1 + wastage) * beamlg * weftdensityON * (width + wlg) * (ends / FDZ) * K / 1000, 10);
                row["WAGKGBRUT"] = qty * 0.001 * 1000;
            }
            WEIGHT = Helper.RoundX(TMV.SUM1/100, 1)*100;
            return IsValidate;
        }
        public virtual void OnYARNChg()
        {
            if (!IsEdit)
                return;
            if (dwMaster == null)
                return;
            if (YARN.Length < 33)
                return;
            TMV.WAGGART = YARN.Substring(1, 20);
            TMV.WAGGVAR = YARN.Substring(21, 3);
            TMV.WAGGFARBE = YARN.Substring(24, 10);
            TMV.WAGABF = 30;
        }
        public override void OnEXAUFChg(string vlaue)
        {
            pos = 0;
            if (!IsCanLoad)
                return;
            if (EXAUF.Length < 33)
                return;
            PM["ART"] = EXAUF.Substring(1, 20);
            PM["VAR"] = EXAUF.Substring(21, 2);
            PM["FARBE"] = EXAUF.Substring(24, 10);
            BEAMART = EXAUF.Substring(0, 23) + EXAUF.Substring(24);
            SResult = TMV.LOADBYWF(PM);
            MergeData(SResult, true);
            WEIGHT = Helper.RoundX(TMV.SUM1 / 100, 1) * 100;
            if (!IsAuto)
                return;
            if (Counts > 0)
                return;
            dwData = TBMain.NewRow();
            TMV.SetUpNewRow(ref dwData);
            dwData["WAGART"] = BEAMART.Substring(1, 20);
            dwData["WAGVAR"] = BEAMART.Substring(21, 2);
            dwData["WAGWCOL"] = BEAMART.Substring(23, 10);
            pos++;
            dwData["WAGPOS"] = Helper.Right("00" + pos, 2);
            TBMain.Rows.InsertAt(dwData, 0);
            vwMaster.View.MoveCurrentToFirst();
            TMV.InsertWASTGA(dwData);
            base.OnEXAUFChg(vlaue);
        }
        public override void NewCommand(object sender, RoutedEventArgs e)
        {
            if (BEAMART == "")
                return;
            dwData = TBMain.NewRow();
            TMV.SetUpNewRow(ref dwData);
            dwData["WAGART"] = BEAMART.Substring(1, 20);
            dwData["WAGVAR"] = BEAMART.Substring(21, 2);
            dwData["WAGWCOL"] = BEAMART.Substring(23, 10);
            pos++;
            dwData["WAGPOS"] = Helper.Right("00" + pos, 2);
            TBMain.Rows.InsertAt(dwData, 0);
            vwMaster.View.MoveCurrentToFirst();
            TMV.InsertWASTGA(dwData);
            base.NewCommand(sender, e);
        }
        string BEAMART = "";
        public void RefreshCommand(object sender, RoutedEventArgs e)
        {
            try
            {
                if (!IsEdit)
                    return;
                if (WTKEY.Trim().Length == 0)
                    return;
                if (BEAMART.Length < 32)
                    return;

                dwData = TBMain.NewRow();
                dwData["WAGMANDANT"] = app.Mandant;
                dwData["WAGART"] = BEAMART.Substring(1, 20);
                dwData["WAGVAR"] = BEAMART.Substring(21, 2);
                dwData["WAGWCOL"] = BEAMART.Substring(23, 10);
                TMV.DWG01(dwData);
                TBMain.Clear();
                PM["SRFNR"] = WTKEY.Trim().PadRight(15);
                SResult = TMV.VWT.LOADBYBS(PM);
                if (SResult.Tables[0].Rows.Count == 0)
                    return;
                DataRow dwWT = SResult.Tables[0].Rows[0];
                for (int YNi = 1; YNi < 100; YNi++)
                {
                    string yarn = dwWT["WTBGARN" + YNi].ToString();
                    double ends = Convert.ToDouble(dwWT["WTBFDZ" + YNi].ToString());
                    if (ends == 0)
                        break;

                    dwData = TBMain.Rows.Add();
                    TMV.SetUpNewRow(ref dwData);
                    dwData["WAGART"] = BEAMART.Substring(1, 20);
                    dwData["WAGVAR"] = BEAMART.Substring(21, 2);
                    dwData["WAGWCOL"] = BEAMART.Substring(23, 10);
                    dwData["WAGPOS"] = Helper.Right("00" + YNi, 2);
                    dwData["WAGFDZ"] = ends;
                    TMV.InsertWASTGA(dwData, app.company);
                }
                vwMaster.View.MoveCurrentToFirst();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
    }
}
